<?php
function em_create_locations_table() {

	global  $wpdb, $user_level;
	$table_name = EM_LOCATIONS_TABLE;

	// Creating the events table
	$sql = "CREATE TABLE ".$table_name." (
		location_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
		location_slug VARCHAR( 200 ) NOT NULL,
		location_name tinytext NOT NULL,
		location_owner bigint(20) unsigned DEFAULT 0 NOT NULL,
		location_address tinytext NOT NULL,
		location_town tinytext NOT NULL,
		location_state VARCHAR( 200 ) NULL,
		location_postcode VARCHAR( 10 ) NULL,
		location_region VARCHAR( 200 ) NULL,
		location_country CHAR( 2 ) NOT NULL,
		location_latitude float DEFAULT NULL,
		location_longitude float DEFAULT NULL,
		location_description text DEFAULT NULL,
		PRIMARY KEY  (location_id),
		KEY (location_state),
		KEY (location_region),
		KEY (location_country),
		KEY (location_slug)
		) DEFAULT CHARSET=utf8 ;";

	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

	$old_table_name = EM_OLD_LOCATIONS_TABLE; //for 3.0
	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name && $wpdb->get_var("SHOW TABLES LIKE '$old_table_name'") != $old_table_name) {
		dbDelta($sql);
		//Add default values
		$wpdb->query("INSERT INTO ".$table_name." (location_name, location_address, location_town, location_state, location_country, location_latitude, location_longitude, location_slug, location_owner) VALUES ('Arts Millenium Building', 'Newcastle Road','Galway','Galway','IE', 53.275, -9.06532, 'arts-millenium-building','".get_current_user_id()."')");
		$wpdb->query("INSERT INTO ".$table_name." (location_name, location_address, location_town, location_state, location_country, location_latitude, location_longitude, location_slug, location_owner) VALUES ('The Crane Bar', '2, Sea Road','Galway','Galway','IE', 53.2692, -9.06151, 'the-crane-bar','".get_current_user_id()."')");
		$wpdb->query("INSERT INTO ".$table_name." (location_name, location_address, location_town, location_state, location_country, location_latitude, location_longitude, location_slug, location_owner) VALUES ('Taaffes Bar', '19 Shop Street','Galway','Galway','IE', 53.2725, -9.05321, 'taffes-bar','".get_current_user_id()."')");
	}else{
		if( get_option('dbem_version') < 4 && get_option('dbem_version') != '' ){
			$wpdb->query('ALTER TABLE wp_em_locations CHANGE location_province location_state TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL');
		}
		dbDelta($sql);
	}
}

?>
